package com.hksj.wmp.controller;

import com.hksj.common.config.annotation.LogLogin;
import com.hksj.common.core.model.Rest;
import com.hksj.wmp.dto.UserInfoDTO;
import com.hksj.wmp.dto.WxLoginDTO;
import com.hksj.wmp.dto.WxPhoneNumberDTO;
import com.hksj.wmp.service.WxLoginService;
import com.hksj.wmp.vo.LoginInfoVO;
import com.hksj.wmp.vo.LoginUserInfoVO;
import com.hksj.wmp.vo.WxUserPhoneNumberVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "微信小程序-登录")
@RequestMapping("/api/wx")
@RestController
public class WxLoginController {
    @Autowired
    private WxLoginService wxLoginService;

    @ApiOperation(value = "小程序登录")
    @PostMapping("/wxLogin")
    @LogLogin
    public Rest<LoginInfoVO> wxLogin(@Validated(value = WxLoginDTO.Login.class) @RequestBody WxLoginDTO dto) {
        LoginInfoVO loginInfoVo = wxLoginService.wxLogin(dto);
        return Rest.ok( "登录成功",loginInfoVo);
    }

    @ApiOperation(value = "小程序登出")
    @GetMapping("/wxLogout")
    public Rest wxLogout() {
        wxLoginService.wxLogout();
        return Rest.ok("注销成功",null);
    }

    @ApiOperation(value = "修改/保存手机号")
    @PostMapping("/updatePhoneNumber")
    public Rest<LoginUserInfoVO> updatePhoneNumber(@RequestBody UserInfoDTO dto){
        LoginUserInfoVO loginUserInfoVO = wxLoginService.updatePhoneNumber(dto);
        return Rest.ok(loginUserInfoVO);
    }

    @ApiOperation(value = "获取微信手机号")
    @PostMapping("/getWxPhoneNumber")
    public Rest<WxUserPhoneNumberVO> getWxPhoneNumber(@RequestBody WxPhoneNumberDTO dto){
        WxUserPhoneNumberVO wxPhoneNumber = wxLoginService.getWxPhoneNumber(dto);
        return Rest.ok(wxPhoneNumber);
    }
}
